﻿using System;

namespace CMWVS.Core
{
    public class RelayCommand<TCommand, TResult> : ICommand<TCommand>, IResult<TResult>
    {
        #region Constructor

        internal RelayCommand()
        {
        }

        public RelayCommand(Func<TCommand, TResult> cmd)
        {
            m_Commmand = cmd;
            m_CanExecute = () => { return true; };
        }

        public RelayCommand(Func<TCommand, TResult> cmd, Func<bool> canExecute)
        {
            m_Commmand = cmd;
            m_CanExecute = canExecute;
        }

        #endregion Constructor

        protected Func<TCommand, TResult> m_Commmand
        {
            get;
            set;
        }

        protected Func<bool> m_CanExecute
        {
            get;
            set;
        }

        #region ICommand<TCommand> Members

        public void Execute(TCommand obj)
        {
            if (this.CanExecute())
            {
                this.Result = m_Commmand(obj);
            }
        }

        public bool CanExecute()
        {
            return m_CanExecute();
        }

        #endregion ICommand<TCommand> Members

        #region IResult<TResult> Members

        public TResult Result
        {
            get;
            private set;
        }

        #endregion IResult<TResult> Members
    }
}